package cn.gok.demolast.controller;


import cn.gok.demolast.dao.StudentMapper;
import cn.gok.demolast.entity.Student;
import cn.gok.demolast.service.StudentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 李xx
 * @since 2022-07-21
 */
@RestController
@RequestMapping("/student")
public class StudentController {
    Student student = new Student();
    @Autowired
    private StudentService studentService;

    //1）查询所有学生信息
    @RequestMapping("/findAll")
    public List<Student> findAllStudent() {
        return studentService.list(null);
    }

    //2）根据ID查询学生信息
    @RequestMapping("/findById")
    public Student findStudentById(@RequestParam Long id) {
        return studentService.getById(id);
    }

    // 3）模糊查询学生信息
    @RequestMapping("/getQuery")
    public List<Student> getQueryWrapper() {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("age", 20);
        List<Student> students = studentService.list(queryWrapper);
        return students;
    }

    //    4）分页查询学生信息
    @RequestMapping("/getpage")
    public List<Student> getpage(@RequestParam int current, @RequestParam int size) {
        Page<Student> studentPage = new Page<>(current, size);
        IPage<Student> page = studentService.page(studentPage, null);
        List<Student> records = page.getRecords();
        return records;
    }

    //5）添加学生信息
    @RequestMapping("/save")
    public boolean saveStudent(@RequestBody Student student) {
        return studentService.save(student);
    }

    //6）修改学生信息
    @RequestMapping("/update")
    public boolean updateStudentById(@RequestBody Student student) {
        return studentService.updateById(student);
    }

    //7）根据ID删除学生
    @RequestMapping("/removeById")
    public boolean removeStudentById(@RequestParam Long id) {
        return studentService.removeById(id);
    }

    //8）批量删除学生
    @RequestMapping("/removeByIds")
    public boolean removeStudentByIds(@RequestBody List<Integer> ids) {
        return studentService.removeByIds(ids);
    }
}

